<?php

namespace app\shop\controller;

use think\Controller;

class Coupon extends Controller
{
    //构造函数
    public function __construct()
    {
        parent::__construct();
        //加载模型
        $this->userModel = model('common/User/User');
        $this->bannerModel = model('common/User/Banner');
        $this->shopModel = model('common/User/Shop');
        $this->cartModel = model('common/User/Cart');
        $this->couponModel = model('common/User/Coupon');
        $this->couponReceiveModel = model('common/User/CouponReceive');
        $this->FreightModel = model('common/User/Mobile');
    }


    // 获取优惠券列表
    public function getCoupon(){
      if($this->request->isPost()){
        $mobile = $this->request->param('mobile');
        $uid = $this->request->param('uid');
        // halt($pid);
        $uData = [
          'id'=>$uid,
          'mobile'=>$mobile
        ];
        $isUser = $this->userModel->where($uData)->select();
        if($isUser){
          $couponList = $this->couponModel->select();
          $this->success('优惠卷列表获取成功',null,$couponList);
          exit;
        }
      }
    }

    // 获取优惠卷详情
    public function getSingleCoupon(){
      if($this->request->isPost()){
        $id = $this->request->param('id');
        $mobile = $this->request->param('mobile');
        $uid = $this->request->param('uid');
        // halt($pid);
        $uData = [
          'id'=>$uid,
          'mobile'=>$mobile
        ];
        $isUser = $this->userModel->where($uData)->select();
        if($isUser){
          $couponList = $this->couponModel->where('id',$id)->select();
          $this->success('优惠卷详情获取成功',null,$couponList);
          exit;
        }
      }
    }

    // 获取用户领取优惠券
    public function receiveCoupon(){
      if($this->request->isPost()){
        $id = $this->request->param('id');
        $mobile = $this->request->param('mobile');
        $uid = $this->request->param('uid');
        // halt($pid);
        $uData = [
          'id'=>$uid,
          'mobile'=>$mobile
        ];
        $isUser = $this->userModel->where($uData)->select();
        if($isUser){
          $data=[
            'cid'=>$id,
            'busid'=>$uid,
          ];
          $receiveRes =  $this->couponReceiveModel->where($data)->select();
          if(!$receiveRes){
            $couponReceive = $this->couponReceiveModel->save($data);
            $this->success('优惠券领取成功',null);
            exit;
          }else{
            $this->error('已经领取过了',null);
            exit;
          }
        }
      }
    }

     // 获取用户领的优惠券
    public function getUserReceive(){
      if($this->request->isPost()){
        $id = $this->request->param('id');
        $mobile = $this->request->param('mobile');
        $uid = $this->request->param('uid');
        // halt($pid);
        $uData = [
          'id'=>$uid,
          'mobile'=>$mobile
        ];
        $isUser = $this->userModel->where($uData)->select();
        if($isUser){
          // 运费
          $freight = $this->FreightModel->where('id',20)->value('value');
          // 运费门槛，满100减15
          $doorsill = $this->FreightModel->where('id',21)->value('value');
          // 关联单个方法
          $receiveCouponRes = $this->couponReceiveModel->with('coupon')->where('busid',$uid)->select();
          // halt($doorsill);
          $receiveData=[
            'freight'=>$freight,
            'doorsill'=>$doorsill,
            'receiveCouponRes'=>$receiveCouponRes
          ];

          $this->success('查询用户领取的优惠卷成功',null,$receiveData);
          exit;
          // halt($receiveCouponRes);
          // $data=[
          //   'cid'=>$id,
          //   'busid'=>$uid,
          // ];
          // $receiveRes =  $this->couponReceiveModel->where($data)->select();
          // if(!$receiveRes){
          //   $couponReceive = $this->couponReceiveModel->save($data);
          //   $this->success('优惠券领取成功',null);
          //   exit;
          // }else{
          //   $this->error('已经领取过了',null);
          //   exit;
          // }
        }
      }
    }

    
    

}

?>

